package com.itruth.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.itruth.base.LocalNotice;

public class HttpUtil {
	
	public static void queryShare(final int shareId, final boolean pullDownRefresh) {
		
		ThreadPool.getIns().addTaskToCachePool(new Runnable() {
			@Override
			public void run() {
				HttpGet req = new HttpGet(Constant.URL + "/queryShare?shareId=" + shareId + "&pullDownRefresh=" + (pullDownRefresh ? 1 : 0));
				HttpClient client = new DefaultHttpClient();
				try {
				
					HttpResponse res = client.execute(req);
					
					if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
						
						String data = EntityUtils.toString(res.getEntity(), HTTP.UTF_8);
						
						Log.i("lanyan", data);
						
						LocalNotice.getIns().sendNotice(LocalNotice.ACTION_QUERY_SHAREDATA, data, Constant.RESPONSE_OK, null);
						
					}
					
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		
	}
	
	public static void reg(final String username, final String password) {
		
		ThreadPool.getIns().addTaskToCachePool(new Runnable() {
			@Override
			public void run() {
				HttpPost req = new HttpPost(Constant.URL + "/reg");
				
				JSONObject param = new JSONObject();
				try {
					param.put("username", username);
					param.put("password", password);
					
					HttpEntity entity = new StringEntity(param.toString(), HTTP.UTF_8);
					req.setEntity(entity);
				
					HttpResponse res = new DefaultHttpClient().execute(req);
					
					if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
						
						String protocolTag = EntityUtils.toString(res.getEntity(), HTTP.UTF_8);
//						Toast.makeText(ItruthApp.getIns(), message, Toast.LENGTH_SHORT).show();
						LocalNotice.getIns().sendNotice(
								LocalNotice.ACTION_REGISTER, null,
								Constant.RESPONSE_OK, protocolTag);
					}
					
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				} 
			}
		});
		
		
		
	}
	
	public static void httpOpts() {
		
		HttpPost request = new HttpPost("http://" + Constant.IP + ":8080/itruth/reg?name=lanyan&age=24");
		HttpClient httpClient = new DefaultHttpClient();
		
		HttpParams httpParameters = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is established.
		int timeoutConnection = 3000;
		HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
		// Set the default socket timeout (SO_TIMEOUT) 
		// in milliseconds which is the timeout for waiting for data.
		int timeoutSocket = 3000;
		HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
		HttpResponse httpResponse = null;
		
		try {
			httpResponse = httpClient.execute(request);
			
//			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//				
//				HttpEntity entity = httpResponse.getEntity();
//				
//				String message = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
//				Toast.makeText(ItruthApp.getIns(), message, Toast.LENGTH_SHORT).show();
//			}
			

//public static final HttpParams createHttpParams() {
//        final HttpParams params = new BasicHttpParams();
//        HttpConnectionParams.setStaleCheckingEnabled(params, false);
//        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);
//        HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);
//        HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);
//        return params;
//    }
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.i("lanyan", "ClientProtocolException");
			
		} catch (IOException e) {
			e.printStackTrace();
			Log.i("lanyan", "IOException" + e.toString());
		}
		
	}
	
}
